package com.two.shopping.controller;

import com.two.shopping.pojo.Orders;
import com.two.shopping.pojo.PageBean;
import com.two.shopping.pojo.Result;
import com.two.shopping.pojo.User;
import com.two.shopping.service.IOrderService;
import com.two.shopping.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class OrderController {
    @Autowired
    private IOrderService iOrderService;

    //用户查询所有订单
    @CrossOrigin
    @GetMapping("/order")
    private Result allSelectOrders(User user, HttpServletRequest request) {
        log.info("查询用户id为{}的订单", request.getHeader("token"));

        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        if (claims != null) {
            String username = (String) claims.get("username");
            user.setUsername(username);
            log.info("{}", claims);
            log.info("{}", username);
        }
        List<Orders> orders = iOrderService.allSelectOrders(user);
        return Result.success(orders);
    }

    //查询所有订单
    @CrossOrigin
    @GetMapping("/orders")
    private Result allSelectOrders(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "5") Integer pageSize) {

        PageBean orders = iOrderService.allSelectOrders(page, pageSize);
        return Result.success(orders);
    }

    //发货
    @CrossOrigin
    @PostMapping("/order/ok")
    private Result fahuo(@RequestBody Orders orders) {
        iOrderService.fahuo(orders);
        return Result.success();
    }

    //收货
    @CrossOrigin
    @PostMapping("order/receive")
    private Result shouhuo(@RequestBody Orders orders) {
        iOrderService.shouhuo(orders);
        return Result.success();
    }
}
